package com.dixon.worddayday.dao;

import com.dixon.worddayday.model.Progress;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProgressSpecification implements Specification<Progress> {
    private Map<String, Object> params;

    public ProgressSpecification(Map<String, Object> params) {
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<Progress> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
        List<Predicate> predicates = new ArrayList<>();

//        // 动态拼接查询条件
//        if (params.containsKey("name")) {
//            String name = (String) params.get("name");
//            if (!StringUtils.isEmpty(name)) {
//                predicates.add(cb.like(root.get("name"), "%" + name + "%"));
//            }
//        }
//        if (params.containsKey("age")) {
//            Integer age = (Integer) params.get("age");
//            if (age != null) {
//                predicates.add(cb.equal(root.get("age"), age));
//            }
//        }
//        if (params.containsKey("email")) {
//            String email = (String) params.get("email");
//            if (StringUtils.isNotBlank(email)) {
//                predicates.add(cb.like(root.get("email"), "%" + email + "%"));
//            }
//        }
//        // 拼接判断空值的查询条件
//        if (params.containsKey("isDeleted") && params.get("isDeleted") != null) {
//            Boolean isDeleted = (Boolean) params.get("isDeleted");
//            if (isDeleted) {
//                predicates.add(cb.isNotNull(root.get("deletedTime")));
//            } else {
//                predicates.add(cb.isNull(root.get("deletedTime")));
//            }
//        }


        if (params.containsKey("customerId")) {
            Integer customerId = (Integer) params.get("customerId");
            if (customerId != null) {
                predicates.add(cb.equal(root.get("customerId"), customerId));
            }
        }

        // 当天
        if(params.containsKey("currentDate") && params.get("currentDate") != null) {
            predicates.add(cb.equal(cb.function("TRUNC", LocalDate.class, root.get("nextReviewTime")), params.get("currentDate")));
        }

        // 当天
        if(params.containsKey("forgotCurrentDate") && params.get("forgotCurrentDate") != null) {
            predicates.add(cb.lessThan(cb.function("TRUNC", LocalDate.class, root.get("nextReviewTime")), (LocalDate)params.get("forgotCurrentDate")));
        }

        if(params.containsKey("notEqualStatus")) {
            predicates.add(cb.notEqual(root.get("status"), params.get("notEqualStatus")));
        }

        if(params.containsKey("inStatus")) {
            predicates.add(root.get("status").in(params.get("inStatus")));
        }

        // 锚点id
        if (params.containsKey("anchorId") && params.get("anchorId") != null) {
            predicates.add(cb.lessThanOrEqualTo(root.get("id"), (Integer) params.get("anchorId")));
        }

        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    }
}
